﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

public partial class Admin_FileUpload : System.Web.UI.Page
{
    public string CourseID
    {
        get
        {
            if (Request.QueryString["CourseID"] != null)
                return Request.QueryString["CourseID"];
            else
                return "-1";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        btnLoadZip.Attributes.Add("style", "visibility: hidden;");
    }

    protected void LoadZipFile(object sender, EventArgs e)
    {
        hidAccessing.Value = "false";
        if (CourseID == "-1")
        {
            lblUploadStatus.ForeColor = System.Drawing.Color.DarkRed;
            lblUploadStatus.Text = "Please save course before uploading cartridge";
            return;
        }

        //bool dc = false;
        string dn = "";

        try
        {
            Ionic.Zip.ZipFile zf = Ionic.Zip.ZipFile.Read(fuLMSFile.FileBytes);
            foreach (Ionic.Zip.ZipEntry ze in zf)
            {
                ze.FileName = ze.FileName.Replace(@"\", "/");
                dn = MakeDirectory(ze.FileName);
                ze.Extract(Server.MapPath("~/Course/Courses/" + CourseID + "/"), true);
            }
            if (CheckFiles())
            {
                lblUploadStatus.ForeColor = System.Drawing.Color.DarkGreen;
                lblUploadStatus.Text = "Cartridge Loaded Successfully";
            }
            else
            {
                lblUploadStatus.ForeColor = System.Drawing.Color.DarkRed;
                lblUploadStatus.Text = "Cartridge Uploaded with Missing Files: <br>" + lblUploadStatus.Text;
            }
        }
        catch (Exception E)
        {
            lblUploadStatus.Text = E.Message;
            lblUploadStatus.ForeColor = System.Drawing.Color.DarkRed;
        }

    }

    private string MakeDirectory(string fname)
    {
        if (!fname.Contains("."))
            return "";
        if (!fname.Contains("/") && !fname.Contains(@"\"))
            return Server.MapPath("~/Course/Courses/") + CourseID + @"\";

        int x = fname.IndexOf('.');
        fname = fname.Remove(x, fname.Length - x);
        x--;
        while (fname[x] != '/' && fname[x] != '\\')
            x--;
        fname = fname.Remove(x, fname.Length - x);

        if (System.IO.Directory.Exists(Server.MapPath("~/Course/Courses/") + CourseID + @"\" + fname))
            return Server.MapPath("~/Course/Courses/") + CourseID + @"\" + fname;

        System.IO.Directory.CreateDirectory(Server.MapPath("~/Course/Courses/") + CourseID + @"\" + fname);

        return Server.MapPath("~/Course/Courses/") + CourseID + @"\" + fname;
    }

    private bool CheckFiles()
    {
        lblUploadStatus.Text = "";
        bool status = true;
        System.Xml.Linq.XDocument xd = System.Xml.Linq.XDocument.Load(Server.MapPath("~/Course/Courses/" + CourseID + "/imsmanifest.xml"));
        foreach (XElement xe in xd.Descendants("file"))
        {
            System.IO.FileInfo fi = null;
            if (xe.Attribute("href") != null)
            {
                fi = new System.IO.FileInfo(Server.MapPath("~/Course/Courses/" + CourseID + "/" + xe.Attribute("href").Value));
                if (!fi.Exists)
                {
                    lblUploadStatus.Text += xe.Attribute("href").Value;
                    status = false;
                }
            }
        }

        return status;
    }
}
